!function (e, t) {
    "object" == typeof exports && "object" == typeof module ? module.exports = t() : "function" == typeof define && define.amd ? define([], t) : "object" == typeof exports ? exports.pomelo = t() : e.pomelo = t()
}(window, function () {
    return function (e) {
        var t = {};

        function n(r) {
            if (t[r]) return t[r].exports;
            var o = t[r] = {i: r, l: !1, exports: {}};
            return e[r].call(o.exports, o, o.exports, n), o.l = !0, o.exports
        }

        return n.m = e, n.c = t, n.d = function (e, t, r) {
            n.o(e, t) || Object.defineProperty(e, t, {enumerable: !0, get: r})
        }, n.r = function (e) {
            "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, {value: "Module"}), Object.defineProperty(e, "__esModule", {value: !0})
        }, n.t = function (e, t) {
            if (1 & t && (e = n(e)), 8 & t) return e;
            if (4 & t && "object" == typeof e && e && e.__esModule) return e;
            var r = Object.create(null);
            if (n.r(r), Object.defineProperty(r, "default", {
                enumerable: !0,
                value: e
            }), 2 & t && "string" != typeof e) for (var o in e) n.d(r, o, function (t) {
                return e[t]
            }.bind(null, o));
            return r
        }, n.n = function (e) {
            var t = e && e.__esModule ? function () {
                return e.default
            } : function () {
                return e
            };
            return n.d(t, "a", t), t
        }, n.o = function (e, t) {
            return Object.prototype.hasOwnProperty.call(e, t)
        }, n.p = "", n(n.s = 2)
    }([function (e, t, n) {
        "use strict";
        e.exports.copyArray = function (e, t, n, r, o) {
            if ("function" == typeof n.copy) n.copy(e, t, r, r + o); else for (var i = 0; i < o; i++) e[t++] = n[r++]
        }
    }, function (e, t, n) {
        "use strict";
        var r = function () {
            function e(e, t) {
                for (var n = 0; n < t.length; n++) {
                    var r = t[n];
                    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
                }
            }

            return function (t, n, r) {
                return n && e(t.prototype, n), r && e(t, r), t
            }
        }();
        var o = n(0).copyArray;
        e.exports = function () {
            function e() {
                !function (e, t) {
                    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
                }(this, e)
            }

            return r(e, null, [{
                key: "strencode", value: function (e) {
                    for (var t = new Uint8Array(3 * e.length), n = 0, r = 0; r < e.length; r++) {
                        var i = e.charCodeAt(r), s = null;
                        s = i <= 127 ? [i] : i <= 2047 ? [192 | i >> 6, 128 | 63 & i] : [224 | i >> 12, 128 | (4032 & i) >> 6, 128 | 63 & i];
                        for (var a = 0; a < s.length; a++) t[n] = s[a], ++n
                    }
                    var u = new Uint8Array(n);
                    return o(u, 0, t, 0, n), u
                }
            }, {
                key: "strdecode", value: function (e) {
                    for (var t = new Uint8Array(e), n = [], r = 0, o = 0, i = t.length; r < i;) t[r] < 128 ? (o = t[r], r += 1) : t[r] < 224 ? (o = ((63 & t[r]) << 6) + (63 & t[r + 1]), r += 2) : t[r] < 240 ? (o = ((15 & t[r]) << 12) + ((63 & t[r + 1]) << 6) + (63 & t[r + 2]), r += 3) : t[r] < 256 && (o = ((7 & t[r]) << 18) + ((63 & t[r + 1]) << 12) + ((63 & t[r + 2]) << 6) + (63 & t[r + 3]), r += 4), n.push(o);
                    return String.fromCodePoint ? String.fromCodePoint.apply(null, n) : String.fromCharCode.apply(null, n)
                }
            }]), e
        }()
    }, function (e, t, n) {
        "use strict";
        var r = n(3);
        e.exports = new r({
            wsCreator: function (e) {
                var t = e.url, n = e.onError, r = e.onOpen, o = e.onMessage, i = e.onClose,
                    s = wx.connectSocket({url: t});
                return s.onError(n), s.onOpen(r), s.onMessage(o), s.onClose(i), s
            }, wsCreatorWeb: function (e) {
                var t = e.url, n = e.onError, r = e.onOpen, o = e.onMessage, i = e.onClose, s = new WebSocket(t);
                return s.onerror = n, s.onopen = r, s.onmessage = o, s.onclose = i, s
            }, urlGenerator: function (e, t) {
                var n = "wss://" + e;
                return t && (n += "/ws/" + t + "/"), n
            }
        })
    }, function (e, t, n) {
        "use strict";
        var r = function () {
            function e(e, t) {
                for (var n = 0; n < t.length; n++) {
                    var r = t[n];
                    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
                }
            }

            return function (t, n, r) {
                return n && e(t.prototype, n), r && e(t, r), t
            }
        }();
        var o = n(4), i = n(5), s = n(1), a = n(6), u = "js-websocket", c = "0.0.1";

        function l(e) {
            var t = i.decode(e);
            return t.body = JSON.parse(s.strdecode(t.body)), t
        }

        function h(e, t, n) {
            var r = e ? i.TYPE_REQUEST : i.TYPE_NOTIFY;
            n = s.strencode(JSON.stringify(n));
            return i.encode(e, r, 0, t, n)
        }

        function f(e, t) {
            var n = "ws://" + e;
            return t && (n += ":" + t), n
        }

        e.exports = function (e) {
            function t(e) {
                !function (e, t) {
                    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
                }(this, t);
                var n = function (e, t) {
                        if (!e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
                        return !t || "object" != typeof t && "function" != typeof t ? e : t
                    }(this, (t.__proto__ || Object.getPrototypeOf(t)).call(this, e)), r = e.wsCreator, o = e.wsCreatorWeb,
                    i = e.urlGenerator, s = void 0 === i ? f : i;
                return n.wsCreator = r, n.wsCreatorWx = r, n.wsCreatorWeb = o, n.urlGenerator = s, n.reconnect = !1, n.reconncetTimer = null, n.reconnectAttempts = 0, n.reconnectionDelay = 5e3, n.handshakeBuffer = {
                    sys: {
                        type: u,
                        version: c,
                        rsa: {}
                    }, user: {}
                }, n.heartbeatInterval = 0, n.heartbeatTimeout = 0, n.nextHeartbeatTimeout = 0, n.gapThreshold = 100, n.heartbeatId = null, n.heartbeatTimeoutId = null, n.handshakeCallback = null, n.callbacks = {}, n.handlers = {}, n.handlers[a.TYPE_HANDSHAKE] = n.handshake.bind(n), n.handlers[a.TYPE_HEARTBEAT] = n.heartbeat.bind(n), n.handlers[a.TYPE_DATA] = n.onData.bind(n), n.handlers[a.TYPE_KICK] = n.onKick.bind(n), n.reqId = 0, n
            }

            return function (e, t) {
                if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function, not " + typeof t);
                e.prototype = Object.create(t && t.prototype, {
                    constructor: {
                        value: e,
                        enumerable: !1,
                        writable: !0,
                        configurable: !0
                    }
                }), t && (Object.setPrototypeOf ? Object.setPrototypeOf(e, t) : e.__proto__ = t)
            }(t, o), r(t, [{
                key: "handshake", value: function (e) {
                    if (501 !== (e = JSON.parse(s.strdecode(e))).code) if (200 === e.code) {
                        this.handshakeInit(e);
                        var t = a.encode(a.TYPE_HANDSHAKE_ACK);
                        this.send(t), this.initCallback && this.initCallback(this.socket)
                    } else this.emit("error", "handshake fail"); else this.emit("error", "client version not fullfill")
                }
            }, {
                key: "handshakeInit", value: function (e) {
                    e.sys && e.sys.heartbeat ? (this.heartbeatInterval = 1e3 * e.sys.heartbeat, this.heartbeatTimeout = 2 * this.heartbeatInterval) : (this.heartbeatInterval = 0, this.heartbeatTimeout = 0), "function" == typeof this.handshakeCallback && this.handshakeCallback(e.user)
                }
            }, {
                key: "heartbeat", value: function (e) {
                    var t = this;
                    if (this.heartbeatInterval) {
                        var n = a.encode(a.TYPE_HEARTBEAT);
                        this.heartbeatTimeoutId && (clearTimeout(this.heartbeatTimeoutId), this.heartbeatTimeoutId = null), this.heartbeatId || (this.heartbeatId = setTimeout(function () {
                            t.heartbeatId = null, t.send(n), t.nextHeartbeatTimeout = Date.now() + t.heartbeatTimeout, t.heartbeatTimeoutId = setTimeout(function () {
                                return t.heartbeatTimeoutCb()
                            }, t.heartbeatTimeout)
                        }, this.heartbeatInterval))
                    }
                }
            }, {
                key: "heartbeatTimeoutCb", value: function () {
                    var e = this, t = this.nextHeartbeatTimeout - Date.now();
                    t > this.gapThreshold ? this.heartbeatTimeoutId = setTimeout(function () {
                        return e.heartbeatTimeoutCb()
                    }, t) : (console.error("server heartbeat timeout"), this.emit("heartbeat timeout"), this.disconnect())
                }
            }, {
                key: "reset", value: function () {
                    this.reconnect = !1, this.reconnectionDelay = 5e3, this.reconnectAttempts = 0, clearTimeout(this.reconncetTimer)
                }
            }, {
                key: "init", value: function (e, t) {
                    this.initCallback = t, this.params = e;
                    var n = e.host, r = e.port, o = e.user, i = e.handshakeCallback, s = e.encode,
                        a = void 0 === s ? h : s, u = e.decode, c = void 0 === u ? l : u, d = e.debugMode,
                        v = e.browserWS;
                    this.encode = a, this.decode = c, this.url = d ? f(n, r) : this.urlGenerator(n, r), v && (this.wsCreator = this.wsCreatorWeb, this.browserWS = v), this.handshakeBuffer.user = o, this.handshakeCallback = i, this.connect()
                }
            }, {
                key: "connect", value: function () {
                    var e = this, t = this.params, n = t.maxReconnectAttempts || 10, r = this.url;
                    this.socket = this.wsCreator({
                        url: r, onError: function (t) {
                            e.emit("io-error", t), console.error("socket error: ", t)
                        }, onOpen: function (t) {
                            e.reconnect && e.emit("reconnect"), e.reset();
                            var n = a.encode(a.TYPE_HANDSHAKE, s.strencode(JSON.stringify(e.handshakeBuffer)));
                            e.send(n)
                        }, onMessage: function (t) {
                            e.browserWS ? function (e, t) {
                                var n = new FileReader;
                                n.onload = function (e) {
                                    var n = e.target.result;
                                    t(n)
                                }, n.readAsArrayBuffer(e)
                            }(t.data, function (t) {
                                e.processPackage(a.decode(t)), e.heartbeatTimeout && (e.nextHeartbeatTimeout = Date.now() + e.heartbeatTimeout)
                            }) : (e.processPackage(a.decode(t.data)), e.heartbeatTimeout && (e.nextHeartbeatTimeout = Date.now() + e.heartbeatTimeout))
                        }, onClose: function (r) {
                            e.emit("close", r), e.emit("disconnect", r), t.reconnect && e.reconnectAttempts < n && (e.reconnect = !0, e.reconnectAttempts++, e.reconncetTimer = setTimeout(function () {
                                return e.connect()
                            }, e.reconnectionDelay), e.reconnectionDelay *= 2)
                        }
                    })
                }
            }, {
                key: "disconnect", value: function () {
                    this.socket && (this.socket.close(), this.socket = !1), this.heartbeatId && (clearTimeout(this.heartbeatId), this.heartbeatId = null), this.heartbeatTimeoutId && (clearTimeout(this.heartbeatTimeoutId), this.heartbeatTimeoutId = null)
                }
            }, {
                key: "request", value: function (e, t, n) {
                    2 === arguments.length && "function" == typeof t ? (n = t, t = {}) : t = t || {}, (e = e || t.route) && (this.reqId++, this.sendMessage(this.reqId, e, t), this.callbacks[this.reqId] = n)
                }
            }, {
                key: "notify", value: function (e, t) {
                    t = t || {}, this.sendMessage(0, e, t)
                }
            }, {
                key: "sendMessage", value: function (e, t, n) {
                    n = this.encode(e, t, n);
                    var r = a.encode(a.TYPE_DATA, n);
                    this.send(r)
                }
            }, {
                key: "send", value: function (e) {
                    this.browserWS ? this.socket.send(e.buffer) : this.socket.send({data: e.buffer})
                }
            }, {
                key: "onData", value: function (e) {
                    e = this.decode(e), this.processMessage(e)
                }
            }, {
                key: "onKick", value: function (e) {
                    e = JSON.parse(s.strdecode(e)), this.emit("onKick", e)
                }
            }, {
                key: "processMessage", value: function (e) {
                    if (!e.id) return this.emit("onMessage", e.route, e.body), void this.emit(e.route, e.body);
                    var t = this.callbacks[e.id];
                    delete this.callbacks[e.id], "function" == typeof t && t(e.body)
                }
            }, {
                key: "processPackage", value: function (e) {
                    if (Array.isArray(e)) for (var t = 0; t < e.length; t++) {
                        var n = e[t];
                        this.handlers[n.type](n.body)
                    } else this.handlers[e.type](e.body)
                }
            }, {
                key: "newInstance", value: function () {
                    return new t({
                        wsCreator: this.wsCreatorWx,
                        wsCreatorWx: this.wsCreatorWx,
                        wsCreatorWeb: this.wsCreatorWeb,
                        urlGenerator: this.urlGenerator
                    })
                }
            }]), t
        }()
    }, function (e, t) {
        function n() {
            this._events = this._events || {}, this._maxListeners = this._maxListeners || void 0
        }

        function r(e) {
            return "function" == typeof e
        }

        function o(e) {
            return "object" == typeof e && null !== e
        }

        function i(e) {
            return void 0 === e
        }

        e.exports = n, n.EventEmitter = n, n.prototype._events = void 0, n.prototype._maxListeners = void 0, n.defaultMaxListeners = 10, n.prototype.setMaxListeners = function (e) {
            if (!function (e) {
                return "number" == typeof e
            }(e) || e < 0 || isNaN(e)) throw TypeError("n must be a positive number");
            return this._maxListeners = e, this
        }, n.prototype.emit = function (e) {
            var t, n, s, a, u, c;
            if (this._events || (this._events = {}), "error" === e && (!this._events.error || o(this._events.error) && !this._events.error.length)) {
                if ((t = arguments[1]) instanceof Error) throw t;
                var l = new Error('Uncaught, unspecified "error" event. (' + t + ")");
                throw l.context = t, l
            }
            if (i(n = this._events[e])) return !1;
            if (r(n)) switch (arguments.length) {
                case 1:
                    n.call(this);
                    break;
                case 2:
                    n.call(this, arguments[1]);
                    break;
                case 3:
                    n.call(this, arguments[1], arguments[2]);
                    break;
                default:
                    a = Array.prototype.slice.call(arguments, 1), n.apply(this, a)
            } else if (o(n)) for (a = Array.prototype.slice.call(arguments, 1), s = (c = n.slice()).length, u = 0; u < s; u++) c[u].apply(this, a);
            return !0
        }, n.prototype.addListener = function (e, t) {
            var s;
            if (!r(t)) throw TypeError("listener must be a function");
            return this._events || (this._events = {}), this._events.newListener && this.emit("newListener", e, r(t.listener) ? t.listener : t), this._events[e] ? o(this._events[e]) ? this._events[e].push(t) : this._events[e] = [this._events[e], t] : this._events[e] = t, o(this._events[e]) && !this._events[e].warned && (s = i(this._maxListeners) ? n.defaultMaxListeners : this._maxListeners) && s > 0 && this._events[e].length > s && (this._events[e].warned = !0, console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.", this._events[e].length), "function" == typeof console.trace && console.trace()), this
        }, n.prototype.on = n.prototype.addListener, n.prototype.once = function (e, t) {
            if (!r(t)) throw TypeError("listener must be a function");
            var n = !1;

            function o() {
                this.removeListener(e, o), n || (n = !0, t.apply(this, arguments))
            }

            return o.listener = t, this.on(e, o), this
        }, n.prototype.removeListener = function (e, t) {
            var n, i, s, a;
            if (!r(t)) throw TypeError("listener must be a function");
            if (!this._events || !this._events[e]) return this;
            if (s = (n = this._events[e]).length, i = -1, n === t || r(n.listener) && n.listener === t) delete this._events[e], this._events.removeListener && this.emit("removeListener", e, t); else if (o(n)) {
                for (a = s; a-- > 0;) if (n[a] === t || n[a].listener && n[a].listener === t) {
                    i = a;
                    break
                }
                if (i < 0) return this;
                1 === n.length ? (n.length = 0, delete this._events[e]) : n.splice(i, 1), this._events.removeListener && this.emit("removeListener", e, t)
            }
            return this
        }, n.prototype.removeAllListeners = function (e) {
            var t, n;
            if (!this._events) return this;
            if (!this._events.removeListener) return 0 === arguments.length ? this._events = {} : this._events[e] && delete this._events[e], this;
            if (0 === arguments.length) {
                for (t in this._events) "removeListener" !== t && this.removeAllListeners(t);
                return this.removeAllListeners("removeListener"), this._events = {}, this
            }
            if (r(n = this._events[e])) this.removeListener(e, n); else if (n) for (; n.length;) this.removeListener(e, n[n.length - 1]);
            return delete this._events[e], this
        }, n.prototype.listeners = function (e) {
            return this._events && this._events[e] ? r(this._events[e]) ? [this._events[e]] : this._events[e].slice() : []
        }, n.prototype.listenerCount = function (e) {
            if (this._events) {
                var t = this._events[e];
                if (r(t)) return 1;
                if (t) return t.length
            }
            return 0
        }, n.listenerCount = function (e, t) {
            return e.listenerCount(t)
        }
    }, function (e, t, n) {
        "use strict";
        var r = function () {
            function e(e, t) {
                for (var n = 0; n < t.length; n++) {
                    var r = t[n];
                    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
                }
            }

            return function (t, n, r) {
                return n && e(t.prototype, n), r && e(t, r), t
            }
        }();
        var o = n(1), i = n(0).copyArray;
        e.exports = function () {
            function e() {
                !function (e, t) {
                    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
                }(this, e)
            }

            return r(e, null, [{
                key: "encode", value: function (e, t, n, r, i) {
                    var d = 1 + (s(t) ? u(e) : 0);
                    if (a(t)) if (n) {
                        if ("number" != typeof r) throw new Error("error flag for number route!");
                        d += 2
                    } else if (d += 1, r) {
                        if ((r = o.strencode(r)).length > 255) throw new Error("route maxlength is overflow");
                        d += r.length
                    }
                    i && (d += i.length);
                    var v = new Uint8Array(d), p = 0;
                    return p = c(t, n, v, p), s(t) && (p = l(e, v, p)), a(t) && (p = h(n, r, v, p)), i && (p = f(i, v, p)), v
                }
            }, {
                key: "decode", value: function (e) {
                    var t = new Uint8Array(e), n = t.length || t.byteLength, r = 0, u = 0, c = null, l = t[r++],
                        h = 1 & l, f = l >> 1 & 7;
                    if (s(f)) {
                        var d = parseInt(t[r]), v = 0;
                        do {
                            u += (127 & (d = parseInt(t[r]))) * Math.pow(2, 7 * v), r++, v++
                        } while (d >= 128)
                    }
                    if (a(f)) if (h) c = t[r++] << 8 | t[r++]; else {
                        var p = t[r++];
                        p ? (c = new Uint8Array(p), i(c, 0, t, r, p), c = o.strdecode(c)) : c = "", r += p
                    }
                    var y = n - r, b = new Uint8Array(y);
                    return i(b, 0, t, r, y), {id: u, type: f, compressRoute: h, route: c, body: b}
                }
            }, {
                key: "TYPE_REQUEST", get: function () {
                    return 0
                }
            }, {
                key: "TYPE_NOTIFY", get: function () {
                    return 1
                }
            }, {
                key: "TYPE_RESPONSE", get: function () {
                    return 2
                }
            }, {
                key: "TYPE_PUSH", get: function () {
                    return 3
                }
            }]), e
        }();
        var s = function (e) {
            return 0 === e || 2 === e
        }, a = function (e) {
            return 0 === e || 1 === e || 3 === e
        }, u = function (e) {
            var t = 0;
            do {
                t += 1, e >>= 7
            } while (e > 0);
            return t
        }, c = function (e, t, n, r) {
            if (0 !== e && 1 !== e && 2 !== e && 3 !== e) throw new Error("unkonw message type: " + e);
            return n[r] = e << 1 | (t ? 1 : 0), r + 1
        }, l = function (e, t, n) {
            do {
                var r = e % 128, o = Math.floor(e / 128);
                0 !== o && (r += 128), t[n++] = r, e = o
            } while (0 !== e);
            return n
        }, h = function (e, t, n, r) {
            if (e) {
                if (t > 65535) throw new Error("route number is overflow");
                n[r++] = t >> 8 & 255, n[r++] = 255 & t
            } else t ? (n[r++] = 255 & t.length, i(n, r, t, 0, t.length), r += t.length) : n[r++] = 0;
            return r
        }, f = function (e, t, n) {
            return i(t, n, e, 0, e.length), n + e.length
        }
    }, function (e, t, n) {
        "use strict";
        var r = function () {
            function e(e, t) {
                for (var n = 0; n < t.length; n++) {
                    var r = t[n];
                    r.enumerable = r.enumerable || !1, r.configurable = !0, "value" in r && (r.writable = !0), Object.defineProperty(e, r.key, r)
                }
            }

            return function (t, n, r) {
                return n && e(t.prototype, n), r && e(t, r), t
            }
        }();
        var o = n(0).copyArray;
        e.exports = function () {
            function e() {
                !function (e, t) {
                    if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
                }(this, e)
            }

            return r(e, null, [{
                key: "encode", value: function (e, t) {
                    var n = t ? t.length : 0, r = new Uint8Array(4 + n), i = 0;
                    return r[i++] = 255 & e, r[i++] = n >> 16 & 255, r[i++] = n >> 8 & 255, r[i++] = 255 & n, t && o(r, i, t, 0, n), r
                }
            }, {
                key: "decode", value: function (e) {
                    for (var t = 0, n = new Uint8Array(e), r = 0, i = []; t < n.length;) {
                        var s = n[t++],
                            a = (r = (n[t++] << 16 | n[t++] << 8 | n[t++]) >>> 0) ? new Uint8Array(r) : null;
                        o(a, 0, n, t, r), t += r, i.push({type: s, body: a})
                    }
                    return 1 === i.length ? i[0] : i
                }
            }, {
                key: "TYPE_HANDSHAKE", get: function () {
                    return 1
                }
            }, {
                key: "TYPE_HANDSHAKE_ACK", get: function () {
                    return 2
                }
            }, {
                key: "TYPE_HEARTBEAT", get: function () {
                    return 3
                }
            }, {
                key: "TYPE_DATA", get: function () {
                    return 4
                }
            }, {
                key: "TYPE_KICK", get: function () {
                    return 5
                }
            }]), e
        }()
    }])
});